package com.weirdor.wxchat.facade.menu;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.weirdor.wxchat.facade.vo.menu.WeixinButton;
import com.weirdor.wxchat.support.exception.OtherAPIException;
import com.weirdor.wxchat.support.exception.WexinReqException;
import com.weirdor.wxchat.support.httpClient.HttpsClient;
import com.weirdor.wxchat.support.utils.JsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weirdor on 2017/11/28.
 */
public class MenuAPI {


    public final static String menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create";

    public final static String menuALL_url = "https://api.weixin.qq.com/cgi-bin/menu/get";

    public final static String del_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/delete";




    /**
     * 创建菜单
     *  button	是	一级菜单数组，个数应为1~3个
     sub_button	否	二级菜单数组，个数应为1~5个
     type	是	菜单的响应动作类型
     name	是	菜单标题，不超过16个字节，子菜单不超过40个字节
     key	click等点击类型必须	菜单KEY值，用于消息接口推送，不超过128字节
     url	view类型必须	网页链接，用户点击菜单可打开链接，不超过256字节
     * @param accessToken
     * @param button  的json字符串
     * @throws WexinReqException
     */
    public static String createMenu(String accessToken,List<WeixinButton> button) throws WexinReqException, OtherAPIException {
        Map<String,Object> map=new HashMap<>();
        map.put("button",button);
        String requestUrl = menu_url+"?access_token="+accessToken;
        String response= HttpsClient.doPost(requestUrl, JsonUtils.to(map));
        JSONObject jsonObject=JSONObject.fromObject(response);
        String msg = jsonObject.getString("errmsg");
        return msg;
    }


    /**
     * 获取所有的菜单
     * @param accessToken
     * @return
     * @throws WexinReqException
     */
    public static List<WeixinButton> getAllMenu(String accessToken) throws WexinReqException, OtherAPIException {
        Map<String,String> map=new HashMap<>();
        map.put("access_token",accessToken);
        String responce=HttpsClient.doGet(menuALL_url,map);
        JSONObject result =JSONObject.fromObject(responce);
        List<WeixinButton> lstButton = null;
        JSONObject menu = result.getJSONObject("menu");
        JSONArray buttons = menu.getJSONArray("button");
        JSONArray subButtons = null;
        lstButton = new ArrayList<WeixinButton>();
        WeixinButton btn = null;
        WeixinButton subBtn = null;
        List<WeixinButton> lstSubButton = null;
        for (int i = 0; i < buttons.size(); i++) {
            btn = (WeixinButton) JSONObject.toBean(buttons.getJSONObject(i),
                    WeixinButton.class);
            subButtons = buttons.getJSONObject(i).getJSONArray("sub_button");
            if (subButtons != null) {
                lstSubButton = new ArrayList<WeixinButton>();
                for (int j = 0; j < subButtons.size(); j++) {
                    subBtn = (WeixinButton) JSONObject.toBean(
                            subButtons.getJSONObject(j), WeixinButton.class);
                    lstSubButton.add(subBtn);
                }
                btn.setSub_button(lstSubButton);
            }
            lstButton.add(btn);
        }
        return lstButton;
    }

    /**
     * 删除所有的菜单
     * @param accessToken
     * @return
     * @throws WexinReqException
     */
    public static String deleteMenu(String accessToken) throws WexinReqException, OtherAPIException {
        Map<String,String> map=new HashMap<>();
        map.put("access_token",accessToken);
        String responce=HttpsClient.doGet(del_menu_url,map);
        JSONObject result =JSONObject.fromObject(responce);
        String msg = result.getString("errmsg");
        return msg;
    }




    public static void main(String[] args) throws WexinReqException, OtherAPIException {
        List<WeixinButton> testsUb = new ArrayList<WeixinButton>();
			WeixinButton w = new WeixinButton();
			w.setType("scancode_push");
			w.setName("扫码推事件");
			w.setKey("rselfmenu_0_1");
			w.setSub_button(null);
			testsUb.add(w);
//        System.out.println(testsUb);
      //  deleteMenu("gq9gIfOS3rkgDLH965rWV2hpMIQj0PXixyB9Hze8Hi13w38b8geWOFMsRg_TeOxlNmj-XFA-U_I1F_SRbVXDcoZdvpvuH9_beKIsqO4NP-S8VsqCN8G2TKL5ufjQdmzeZITjAHATCN");
        System.out.println(createMenu("gq9gIfOS3rkgDLH965rWV2hpMIQj0PXixyB9Hze8Hi13w38b8geWOFMsRg_TeOxlNmj-XFA-U_I1F_SRbVXDcoZdvpvuH9_beKIsqO4NP-S8VsqCN8G2TKL5ufjQdmzeZITjAHATCN",testsUb));}
        // System.out.println(JSON.toJSON(getAllMenu("gq9gIfOS3rkgDLH965rWV2hpMIQj0PXixyB9Hze8Hi13w38b8geWOFMsRg_TeOxlNmj-XFA-U_I1F_SRbVXDcoZdvpvuH9_beKIsqO4NP-S8VsqCN8G2TKL5ufjQdmzeZITjAHATCN")));
    }


